import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    isLoading: false,
    userstate:JSON.parse(localStorage.getItem('user')) || {},//用户信息
    loginState:false||(Boolean(JSON.parse(localStorage.getItem('user'))))//登录状态
  },
  mutations: {
    UPDATE_LOADING  (state, status) {
      state.isLoading = status.isLoading; //传入额外的参数，即 mutation 的 载荷（payload）：
    },
    userSignin(state, user){
      localStorage.setItem('openId', user.openId);//更新
      var userstate=state.userstate;
      localStorage.setItem('user', JSON.stringify(user));//向浏览器存储写入用户信息
      state.loginState=true;
      Object.assign(userstate, user);
    },
    userSignout(state) {
      localStorage.removeItem('user');
      localStorage.removeItem('openId');
      state.loginState=false;
      Object.keys(state.userstate).forEach(k => Vue.delete(state.userstate, k))
    }
  },
  actions:{
    userSignin:function({commit}, user){ //登录
     // console.log(user);
      var loginFlag=true; //false
      if(loginFlag){
        commit("userSignin", user);
      }
    },
    userSignout:function({commit}) { //退出
      commit("userSignout");
    }
  }
  });
export default store;
